/**
 * PSSDetail - The all inclusive plugin for product detail pages for the PSS system
 * Link to the project page and/or repositorry
 * Copyright (c) 2009 Printing Storefront Solutions
 * Author Chris Beaven
 * Author Url www.saobart.com
 * REQUIRES CLUETIP PLUGIN
 * REQUIRES getUrlParam PLUGIN
 * REQUIRES autofill PLUGIN
 */
(function($){
 $.fn.pssDetail = function(options) {
    
  var defaults = {
  	estShippingInputText:		"Enter Zip Code",
  	estShippingDefaultTextColor:"#999",
  	estShippingActiveTextColor:	"#000",
	pageTitleMenu:				false,	//true/false, toggle navigation in the product detail menu
	pageTitleMenuHeading:		"All Products",	//text, pagetitle roll over activation link text
	addToCartAgreement:			false,	//true/false, toggle text next to the add to cart/customize button
	addToCartAgreementLink:		"list.aspx",	//text, link to agreement information page
	addToCartAgreementLinkText: "Terms Of Use",	//text, agreement link text
	detailToolTips: 			false,	//true/false, toggle tooltips
	spotColorsOnInfo: 			false,	//true/false, toggle spot colors on info buttons
	setMinimumQuantity: 		0,	//digit, set qty field 1 to a minimum number 0 = off
	enhancedPricing:			true,	//true/false, toggle enhanced pricing
	totalCostText:				"Total Cost",
	unitPriceText:				0,  //set unit price text per product - ["3629|Per copy","1234|Per Page","4567|Per stack"] - ["productid|text"] - 0=off
	youSavedTest:				"You Saved",
	everyDayPriceText:			"Low Everyday Price",
	unitPriceQuantity: 			0,	//set unit price divider per product - ["3629|3","1234|2","4567|1","3628|5"] - ["productid|qtyfield"] - 0=off
	setMinimumPurchasePrice:    0,	//set a minimum purchase price - ["3629|25.00","1234|36.00","4567|124.00","3628|25.00"] - ["productid|minprice"] - 0=off
	minimumPurchaseAlertText:	"The minimum purchase amount for this product is $price please adjust your calculator.",//min price alert statement use - price - to report the min price
	setQtyNoDisplay:			0,  //set qty fields to no-display per product - ["3629|3","1234|2","4567|4","3628|5"] - ["productid|qtyfield"] - 0=off
	setQtyMath:					0,  //set math for qty fields per product - ["17370|(Q2+Q3)*Q4:Q5", "17124|(Q2+Q3)*Q4:Q5"] - ["productid|math:reportfield"] - 0=off
	noFileUploadAlertText:		"This Product Requires that you upload a file. Please select your file(s) then click",
	useGetShipDate:				false, //true/false, displays shiping date on calc 
	beforeShipDateText:			"Ships by...",
	printerGMT: 				0, //set the printers GMT EST = -5, CST = -6, MST = -7, PST = -8 -- 0 = off
	setCutoffTime: 				0, //military time 13 = 1pm
	setMinimumPrice:			0,  //- ["productid|minPrice:fieldUsed"] - 0=off
	debug: 						false
  };
  
  var options = $.extend(defaults, options);
    
  return this.each(function() {
  
   obj = $(this);
   
   
	if (options.debug == true){
		if($("#calcDebugArea").length == 0){
			$("body").append("<div id='calcDebugArea' style='overflow:scroll;position:fixed;width:300px;height:600px;background:#fff;border:1px #f00 solid;bottom:0;left:0;padding:15px;'>Debug Area:<br/><br/></div>");
		}
	}
	
   	//Remove line breaks in file upload fields and other places
	$("#ctl00_ContentPlaceHolder1_PriceCalcCtl1_divResults br", obj).remove();
	$("#ctl00_ContentPlaceHolder1_PriceCalcCtl1_lblPriceLabel", obj).remove();
   
   //remove file upload wrapper if no file upload
   if($('#detailPage_uploadFile').html() == ''){
   		$('#detailPage_uploadFile').remove();
   }
   //remove ship calc wrapper if ship calc is not there
   if ($('#ctl00_ContentPlaceHolder1_pnlShippingCalculator').length == ''){
   		$('#detailPage_shipCalc').remove();
   }
   
	//edit estimate shipping input, remove "postal code" and add auto fill //needs autofill function also
	$('#detailPage_shippingCalculatorPostalCode', obj).hide();
	$('#ctl00_ContentPlaceHolder1_txtPostalCode', obj).autofill({
		value: options.estShippingInputText, 
		defaultTextColor: options.estShippingDefaultTextColor, 
		activeTextColor: options.estShippingActiveTextColor
	});
	
	$('#ctl00_ContentPlaceHolder1_txtPostalCode', obj).live('keypress', function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			AjaxNS.AR('ctl00$ContentPlaceHolder1$btnShipCost', '', 'ctl00_ContentPlaceHolder1_RadAjaxPanel2', event);
			return false;
		} else {
			return true;
		}
	});
   
	//additon of all product navigation in the product detail header
	if(options.pageTitleMenu == true){
		var pageTitle = $('#detailPage_pageTitle', obj);
		var prodNavHtml = $('#ctl00_blProductNavigation', obj).html();
		pageTitle.append('<div id="detailNav">' + options.pageTitleMenuHeading + '<ul>' + prodNavHtml + '</ul></div>');
		$('#detailNav', obj).hover(
			function () {$(this).children().show();}, 
			function () {$(this).children().hide();}
		);
	}
   
	
	//add to cart button agreement link
	if(options.addToCartAgreement == true && $('#addToCartAgreement').length == 0){
		var addToCartButtonText = $('#ctl00_ContentPlaceHolder1_btnSubmit', obj).val();
		var addToCartArea = $('#detailPage_addToCart', obj);
		addToCartArea.append('<div id="addToCartAgreement"> By clicking ' + addToCartButtonText + 
			', I agree to the <a href="' + options.addToCartAgreementLink + 
			'" title="View ' + options.addToCartAgreementLinkText + 
			'" target="_blank">' + options.addToCartAgreementLinkText + 
			'</a>.</div> ');
	}
	
	
	//product detail tooltips
	if(options.detailToolTips == true){
		var toolTipWrap = $('.calc-detail-image-wrap', obj);
		if(toolTipWrap.length){
			$('.calc-img-link > img', obj).attr('title', '');
			$('.calc-img-link', obj).cluetip({ 
				closePosition: 'title', 
				arrows: true, 
				width: 450, 
				attribute: 'href',
				cluetipClass: 'jtip',
				dropShadow: false,
				sticky: true,
				mouseOutClose: true
			}); 
		}else{
			toolTipWrap.hide();
		}
	}
	
	
	//product detail, notice for no file uploaded
	var fileUploadInstructions = $('#ctl00_ContentPlaceHolder1_lblUploadInstructions', obj);
	if (fileUploadInstructions.length){
		$("#ctl00_ContentPlaceHolder1_panelFileUpload br", obj).remove();
		if (fileUploadInstructions.attr("style")){
			alert(options.noFileUploadAlertText + ' "' + addToCartButtonText + '"');
		}
	}
	

	//spot colors on pricing calculator info buttons
	if(options.spotColorsOnInfo == true){
		$('.calc-img-link', obj).each(function(){						  
			var myBox = $(this);						  
			var calcImgLink = $(this).attr('href');
			if (calcImgLink.match(/[a-zA-Z]{2,}_Gray_\d*/)){
				var calcImgLinkPms = calcImgLink.match(/[a-zA-Z]{2,}_Gray_\d*/);
			}else if (calcImgLink.match(/Pantone_[a-zA-Z]{2,}/)){
				var calcImgLinkPms = calcImgLink.match(/Pantone_[a-zA-Z]{2,}/);
			}else if (calcImgLink.match(/Process_[a-zA-Z]{2,}/)){
				var calcImgLinkPms = calcImgLink.match(/Process_[a-zA-Z]{2,}/);
			}else if (calcImgLink.match(/Reflex_[a-zA-Z]{2,}/)){
				var calcImgLinkPms = calcImgLink.match(/Reflex_[a-zA-Z]{2,}/);
			}else if (calcImgLink.match(/Black/)){
				var calcImgLinkPms = calcImgLink.match(/Black/);
			}else if (calcImgLink.match(/Red_\d{2,4}/)){
				var calcImgLinkPms = calcImgLink.match(/Red_\d{2,4}/);
			}else if (calcImgLink.match(/[a-zA-Z]*_Red/)){
				var calcImgLinkPms = calcImgLink.match(/[a-zA-Z]*_Red/);
			}else if (calcImgLink.match(/Violet/)){
				var calcImgLinkPms = calcImgLink.match(/Violet/);
			}else if (calcImgLink.match(/[a-zA-Z]*_\d{2,4}/)){
				var calcImgLinkPms = calcImgLink.match(/[a-zA-Z]*_\d{2,4}/);
			}
			if (calcImgLinkPms != null){
				var colorName = calcImgLinkPms; 
				$.ajax({
                 	type: "GET",
                 	url: "/sites/952/files/calc_images/pmsdata.xml",
                 	dataType: "xml",
                 	success: function(xml) {
					 	$(xml).find('name').each(function(){
                         	if ($(this).text() == colorName){ 
							 	var hexColor = $(this).siblings().text();
								if (calcImgLinkPms != null){
									myBox.css("background-color", hexColor);
								}
						 	}
                     	}); //close each(
                 	}
             	}); //close $.ajax(
			}//end if				
		}); //close each(
	}
	
	var qtyOneWrap = $('.PriceCalc_Quantity', obj);
	var qtyOneInput = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_tbQuantity', obj);
	var qtyTwoInput = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_tbQuantity2', obj);
	var qtyThreeInput = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_tbQuantity3', obj);
	var qtyFourWrap = $('.PriceCalc_Quantity4', obj);
	var qtyFourInput = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_tbQuantity4', obj);
	var qtyFiveInput = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_tbQuantity5', obj);
	var qtyOneDD = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddQuantity :selected', obj);
	var qtyTwoDD = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddQuantity :selected', obj);
	var qtyThreeDD = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddQuantity :selected', obj);
	var qtyFourDD = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddQuantity :selected', obj);
	var currentProductID = $(document).getUrlParam("p");
	if($('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_lblFinalPrice', obj).length){
		var originalCalcPrice = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_lblFinalPrice', obj).html();
		totalPrice = originalCalcPrice.replace(/[$,]/g, "");
	}else{totalPrice = 0;}
	var addToCartButton = $('#ctl00_ContentPlaceHolder1_btnSubmit', obj);
	var priceResultWrap = $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_divResults', obj);
	var calcImageParam = $('#calc-img-param', obj);
	
	//set min qty
	if(options.setMinimumQuantity != 0){
		qtyOneInput.blur(function(){
			if ($(this).val() < options.setMinimumQuantity){
				$(this).val(options.setMinimumQuantity);
			}
		});
	}



   
   
   //setting minimum price per product
   
   //matching product id and setting minimum price
	if(options.setMinimumPrice.length){
		i = 0;
		while (i < options.setMinimumPrice.length){
			

			setMinPriceArray = options.setMinimumPrice[i];
			setMinPriceSplit = setMinPriceArray.indexOf("|");
			setMinRepPriceSplit = setMinPriceArray.indexOf(":");
			theMinPriceSet = setMinPriceArray.slice(setMinPriceSplit+1, setMinRepPriceSplit);
			qtyFieldToAdjustPrice = setMinPriceArray.slice(setMinRepPriceSplit+1);
			prodIdNumber = setMinPriceArray.slice(0,setMinPriceSplit);
			
			if(qtyFieldToAdjustPrice == "Q1"){
				currentValue = qtyOneInput.val();
			}else if(qtyFieldToAdjustPrice == "Q2"){
				currentValue = qtyTwoInput.val();
			}else if(qtyFieldToAdjustPrice == "Q3"){
				currentValue = qtyThreeInput.val();
			}else if(qtyFieldToAdjustPrice == "Q4"){
				currentValue = qtyFourInput.val();
			}else if(qtyFieldToAdjustPrice == "Q5"){
				currentValue = qtyFiveInput.val();
			}
			
			if(prodIdNumber == currentProductID){
			
				//setup variables
				originalCalcPrice = $('.calculatorPrice').html();
				totalPrice = parseFloat(originalCalcPrice.replace(/[$,]/g, ""));
				
				//debug
				if(options.debug){$("#calcDebugArea").append("The minimum price set: " + theMinPriceSet + "<br/>The QTY field to adjust: " + qtyFieldToAdjustPrice + "<br/> Current Product ID number: " + prodIdNumber + "<br/>total price:" + totalPrice + "<br/>----<br/>");}
				
				//hide calc for processing 
				//calcHeight = $("#detailPage_calculator").height();
				//$("#ctl00_ContentPlaceHolder1_pnlPricingCalculator").css('min-height', calcHeight);
				$("#ctl00_ContentPlaceHolder1_RadAjaxPanel1").css({position: 'absolute', left: '-999em'});

				if (totalPrice < theMinPriceSet){
					//debug
					if(options.debug){$("#calcDebugArea").append("Entering totalPrice < theMinPriceSet:<br/>----<br/>");}
			
					realPrice = totalPrice - (currentValue / 100);
			
					differenceInPrice = (theMinPriceSet * 100) - Math.round(realPrice * 100);
			
					if(qtyFieldToAdjustPrice == "Q1"){
						qtyOneInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q2"){
						qtyTwoInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q3"){
						qtyThreeInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q4"){
						qtyFourInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q5"){
						qtyFiveInput.val(Math.abs(differenceInPrice));
					}
					
					setTimeout('AjaxNS.AR(\'ctl00$ContentPlaceHolder1$PriceCalcCtl1$tbQuantity\',\'\', \'ctl00_ContentPlaceHolder1_RadAjaxPanel1\', event)', 0)
					
					addLoadEvent(function() {
    					setTimeout('AjaxNS.AR(\'ctl00$ContentPlaceHolder1$PriceCalcCtl1$tbQuantity\',\'\', \'ctl00_ContentPlaceHolder1_RadAjaxPanel1\', event)', 0)
					});
				}
		
				if (totalPrice > theMinPriceSet && currentValue > 0){
					//debug
					if(options.debug){$("#calcDebugArea").append("totalPrice > theMinPriceSet && qtyFourInput.val() > 0:<br/>----<br/>");}
			
					realPrice = totalPrice - (currentValue / 100);
			
					differenceInPrice = (theMinPriceSet * 100) - Math.round(realPrice * 100);
			
					if (differenceInPrice < 0){
						differenceInPrice = 0;
					}
			
					if(qtyFieldToAdjustPrice == "Q1"){
						qtyOneInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q2"){
						qtyTwoInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q3"){
						qtyThreeInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q4"){
						qtyFourInput.val(Math.abs(differenceInPrice));
					}else if(qtyFieldToAdjustPrice == "Q5"){
						qtyFiveInput.val(Math.abs(differenceInPrice));
					}
			
					setTimeout('AjaxNS.AR(\'ctl00$ContentPlaceHolder1$PriceCalcCtl1$tbQuantity\',\'\', \'ctl00_ContentPlaceHolder1_RadAjaxPanel1\', event)', 0)
				}
		
				setTimeout(function() {
 					$("#ctl00_ContentPlaceHolder1_RadAjaxPanel1").css({position: 'static', left: '0'});
				}, 1500);
				
				
			}//end if prodIdNumber == currentProductID
			i++;
		}//end while i < options.setMinimumPrice.length
	}//end if
   
   
   // a better onload event, used with setting min price on calculator 
function addLoadEvent(func) {
  		var oldonload = window.onload;
  		if (typeof window.onload != 'function') {
			window.onload = func;
  		} else {
    		window.onload = function() {
      			if (oldonload) {
        			oldonload();
      			}
      		func();
    		}
  		}
	}


	
	//matching product id and setting minimum price
	if(options.setMinimumPurchasePrice.length){
		i = 0;
		while (i < options.setMinimumPurchasePrice.length){
			setMinArray = options.setMinimumPurchasePrice[i];
			setMinSplit = setMinArray.indexOf("|");
			minPrice = setMinArray.slice(setMinSplit+1);
			prodIdNumber = setMinArray.slice(0,setMinSplit);
			addToCartButton.unbind();
			if(prodIdNumber == currentProductID && totalPrice < minPrice){
				var minOrderUnits = (minPrice-totalPrice)*100;
				bindaddtocart(minOrderUnits, minPrice);
			}
			i++;
		}
	}
	
	
	function bindaddtocart(units, price){
		addToCartButton.bind('click', function(){
			options.minimumPurchaseAlertText = options.minimumPurchaseAlertText.replace(/price/gi,price);
			alert(options.minimumPurchaseAlertText);
			return false;
		});
	}

	//setting no display on quantity fields
	if(options.setQtyNoDisplay.length){
		i = 0;
		while (i < options.setQtyNoDisplay.length){
			setMinArrayND = options.setQtyNoDisplay[i];
			setMinSplitND = setMinArrayND.indexOf("|");
			qtyFieldToHide = setMinArrayND.slice(setMinSplitND+1);
			prodIdNumberND = setMinArrayND.slice(0,setMinSplitND);
			
			if(prodIdNumberND == currentProductID){
				if(qtyFieldToHide == 1){$('.PriceCalc_Quantity, .calc-qty li:eq(0)').hide();}
				if(qtyFieldToHide == 2){$('.PriceCalc_Quantity2, .calc-qty li:eq(1)').hide();}
				if(qtyFieldToHide == 3){$('.PriceCalc_Quantity3, .calc-qty li:eq(2)').hide();}
				if(qtyFieldToHide == 4){$('.PriceCalc_Quantity4, .calc-qty li:eq(3)').hide();}
				if(qtyFieldToHide == 5){$('.PriceCalc_Quantity5, .calc-qty li:eq(4)').hide();}
			}
			i++;
		}
	}

	//set qty field multiplication
	if(options.setQtyMath.length){
		i = 0;
		while (i < options.setQtyMath.length){
			setMinArrayQM = options.setQtyMath[i];
			setMinSplitQM = setMinArrayQM.indexOf("|");
			setMinRepSplitQM = setMinArrayQM.indexOf(":");
			mathEquation = setMinArrayQM.slice(setMinSplitQM+1, setMinRepSplitQM);
			qtyToReportTo = setMinArrayQM.slice(setMinRepSplitQM+1);
			prodIdNumberQM = setMinArrayQM.slice(0,setMinSplitQM);
			
			if(prodIdNumberQM == currentProductID){
			if(options.debug){
				$("#calcDebugArea").append('set math options = '+options.setQtyMath+'\n position in array = '+setMinArrayQM+'\n index of | split = '+setMinSplitQM+'\n index of : split = '+setMinRepSplitQM+'\n qty to report to = '+qtyToReportTo+'\n product id number = '+prodIdNumberQM + "<br/>----<br/>");
				}
				operation = mathEquation.replace(/Q1/g, qtyOneInput.val())
				.replace(/Q2/g, qtyTwoInput.val())
				.replace(/Q3/g, qtyThreeInput.val())
				.replace(/Q4/g, qtyFourInput.val())
				.replace(/Q5/g, qtyFiveInput.val());

				sumOf = eval(operation);
				//alert('operation = '+operation+'\n sumof = '+sumOf); //debug info
				if(qtyToReportTo == 'Q1'){qtyOneInput.val(sumOf);}
				else if(qtyToReportTo == 'Q2'){qtyTwoInput.val(sumOf);}
				else if(qtyToReportTo == 'Q3'){qtyThreeInput.val(sumOf);}
				else if(qtyToReportTo == 'Q4'){qtyFourInput.val(sumOf);}
				else if(qtyToReportTo == 'Q5'){qtyFiveInput.val(sumOf);}
				else{ }
				break;
			}
			i++;
		}
	
	}



		
	$('.calculatorPrice', obj).remove();

	//determine unit price divider
	if(options.unitPriceQuantity.length){
		i = 0;
		while (i < options.unitPriceQuantity.length){
			setMinArrayUP = options.unitPriceQuantity[i];
			setMinSplitUP = setMinArrayUP.indexOf("|");
			qtyFieldToDivideBy = setMinArrayUP.slice(setMinSplitUP+1);
			prodIdNumberUP = setMinArrayUP.slice(0,setMinSplitUP);
			
			if(prodIdNumberUP == currentProductID){
				if(qtyFieldToDivideBy == 1 || qtyFieldToDivideBy == ""){
					if(qtyOneDD.length){chosenQtyValue = qtyOneDD.val();}
					else{chosenQtyValue = qtyOneInput.val();}
				}
				if(qtyFieldToDivideBy == 2){chosenQtyValue = qtyTwoInput.val();}
				if(qtyFieldToDivideBy == 3){chosenQtyValue = qtyThreeInput.val();}
				if(qtyFieldToDivideBy == 4){chosenQtyValue = qtyFourInput.val();}
				if(qtyFieldToDivideBy == 5){chosenQtyValue = qtyFiveInput.val();}
				break;
			}else{
				if(qtyOneDD.length){
					chosenQtyValue = qtyOneDD.val();
				}else{
					chosenQtyValue = qtyOneInput.val();
				}
			}
			i++;
		}
	} else {
		if(qtyOneDD.length){
			chosenQtyValue = qtyOneDD.val();
		}else{
			chosenQtyValue = qtyOneInput.val();
		}
	}
	
	if (options.debug){$("#calcDebugArea").append('Qty 1 input = ' + qtyOneInput.val() + "<br/>----<br/>");}
	if (options.debug){$("#calcDebugArea").append('Qty 1 dd = ' + qtyOneDD.val() + "<br/>----<br/>");}
	
	
	//unit price text per product
	if(options.unitPriceText.length){
		i = 0;
		while (i < options.unitPriceText.length){
			setMinArrayUPT = options.unitPriceText[i];
			setMinSplitUPT = setMinArrayUPT.indexOf("|");
			unitPriceText = setMinArrayUPT.slice(setMinSplitUPT+1);
			prodIdNumberUPT = setMinArrayUPT.slice(0,setMinSplitUPT);
			
			if(prodIdNumberUPT == currentProductID){
				unitPriceTextOut = unitPriceText;
				break;
			}else{
				unitPriceTextOut = "Each";
			}
			i++;
		}
	}



		//add text to current price
		priceResultWrap.append('<div id="totalcost"><ul><li class="costleft">' + options.totalCostText + '</li><li class="costright originalCost">' + originalCalcPrice + '</li></ul></div>');
		
		//devide total price by the chosen qty and display under total price
		if(typeof(chosenQtyValue) == "undefined" || chosenQtyValue == 0){chosenQtyValue = 1;};
		if(typeof(unitPriceTextOut) == "undefined"){unitPriceTextOut = "Each";};
		
		if(options.setMinimumPrice.length){
			division = totalPrice/chosenQtyValue;
		}else{
			division = totalPrice/chosenQtyValue;
		}
		
	if (options.debug){$("#calcDebugArea").append('Unit price divide by ' + chosenQtyValue + "<br/>----<br/>");}
	
		priceResultWrap.append('<div id="finalcost"><ul><li class="costleft">' + unitPriceTextOut + ' </li><li class="costright">$' + division.toFixed(4) + '</li></ul></div>');
		
		
	if(options.setMinimumPrice.length){
		i = 0;
		while (i < options.setMinimumPrice.length){
			setMinPriceArray = options.setMinimumPrice[i];
			setMinPriceSplit = setMinPriceArray.indexOf("|");
			setMinRepPriceSplit = setMinPriceArray.indexOf(":");
			theMinPriceSet = setMinPriceArray.slice(setMinPriceSplit+1, setMinRepPriceSplit);
			qtyFieldToAdjustPrice = setMinPriceArray.slice(setMinRepPriceSplit+1);
			prodIdNumber = setMinPriceArray.slice(0,setMinPriceSplit);
			if(prodIdNumber == currentProductID){
				$("#finalcost ul").append("<li class='minPriceMessage'>($" + theMinPriceSet + " Minimum Order)</li>");
			}
			i++;
		}
	}
		
		

		var disMult = new Array();
			disMult[0] = 1.000000;
			disMult[5] = 1.052632;
			disMult[10] = 1.111111;
			disMult[15] = 1.176471;
			disMult[20] = 1.250000;
			disMult[25] = 1.333333;
			disMult[30] = 1.428571;
			disMult[35] = 1.538462;
			disMult[40] = 1.666667;
			disMult[45] = 1.818182;
			disMult[50] = 2.000000;
			disMult[55] = 2.222222;
			disMult[60] = 2.500000;
			disMult[65] = 2.857143;
			disMult[70] = 3.333333;
			disMult[75] = 4.000000;
			disMult[80] = 5.000000;
			disMult[85] = 6.666667;
			disMult[90] = 10.000000;
			disMult[95] = 20.000000;

	if(options.enhancedPricing == true){
		$(".calc-parm-inputs span").each(function(){							 
			if ($(this).text().match(/Discount (\d+)/)){
				discountValue = $(this).text().slice(-2);
				//discount price Discount 10%
				discountPrice = totalPrice * (disMult[discountValue] - 1);
				priceResultWrap.prepend('<div id="discount-price"><ul><li class="costleft">' + options.youSavedTest + ' </li><li class="costright">-$' + discountPrice.toFixed(2) + '</li></ul></div>');
				//before discount price
				preSavingsPrice = totalPrice * disMult[discountValue];
				priceResultWrap.prepend('<div id="prediscount-price"><ul><li class="costleft">' + options.everyDayPriceText + ' </li><li class="costright">$' + preSavingsPrice.toFixed(2) + '</li></ul></div>');
			}
		});
	}
		
		//display none on calc params that have discountprice in label
		$('.PriceCalc_CalcParm').each(function(){								 
			if ($(this).html().match(/Discount (\d+)/)){	
				$(this).hide();
			}
			else if ($(this).html().match(/_Spec_/)){
				$(this).hide();
			}
			else if ($(this).html().match(/Product ID/)){
				$(this).hide();
			}
		});
		//display none on grid params that have discountprice in label
		$('.PriceCalc_GridParm').each(function(){								 
			if ($(this).html().match(/Discount (\d+)/)){	
				$(this).hide();
			}
			else if ($(this).html().match(/_Spec_/)){
				$(this).hide();
			}
			else if ($(this).html().match(/Product ID/)){
				$(this).hide();
			}
		});
		
		$('.calc-img-link').each(function(){							  
			var href = $(this).attr("href");
			if (href.match(/Spec_/)){
				$(this).parent().hide();
			}
			if (href.match(/Discount/)){
				$(this).parent().hide();
			}
			if (href.match(/Product_ID/)){
				$(this).parent().hide();
			}
		});
		
		$("#ctl00_ContentPlaceHolder1_PriceCalcCtl1_calcParmInputs").append('<div id="yesdisplay-area"><h6>Product Details...</h6><ul id="yesdisplay-elements"></ul></div>');
		
		for (i=1; i<9; i++){
			if ($('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddCalcParm_Spec_' + i + ' :selected').text() != ""){
				$("#yesdisplay-elements").append("<li>" + $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddCalcParm_Spec_' + i + ' :selected').text() + "</li>");
			}
			if ($('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddGridParm_Spec_' + i + ' :selected').text() != ""){
				$("#yesdisplay-elements").append("<li>" + $('#ctl00_ContentPlaceHolder1_PriceCalcCtl1_ddGridParm_Spec_' + i + ' :selected').text() + "</li>");
			}
		}
		
		if ($('#yesdisplay-elements').html() == ""){
			$('#yesdisplay-area').hide();
		}


	today = new Date();
	currentHour = today.getHours();
	customerGMT = -today.getTimezoneOffset()/60;
	busdays = 0;

	//printer gmt plus customer gmt to get offset
	if(options.printerGMT != 0){
		gMTOffset = options.printerGMT - customerGMT;
	}else{
		gMTOffset = 0;
	}

	//if current customer time plus offset is => then the set time add a day
	if(options.setCutoffTime != 0){
		if(currentHour + gMTOffset >= options.setCutoffTime){
			busdays+=1;
		}
	}
	
	
	
	Number.prototype.mod = function(n){
		return ((this%n)+n)%n;
	}
	Date.prototype.addBusDays = function(dd) {
		var wks = Math.floor(dd/5);
		var dys = dd.mod(5);
		var dy = this.getDay();
		if (dy === 6 && dys > -1) {
	   		if (dys === 0) {dys-=2; dy+=2;}
	   			dys++; dy -= 6;}
		if (dy === 0 && dys < 1) {
	   		if (dys === 0) {dys+=2; dy-=2;}
	   			dys--; dy += 6;}
		if (dy + dys > 5) dys += 2;
		if (dy + dys < 1) dys -= 2;
		this.setDate(this.getDate()+wks*7+dys);
		return this.toDateString();
	}


	
	//estimate the ship date function //uses calculateBusinessDays function
	if(options.useGetShipDate == true){
		daysToShip = 0;
		$('.PriceCalc_dd_AllOpts').find(':selected').each(function(){			 
			if ($(this).text().match(/1 Day/)){daysToShip = 1;}
			else if ($(this).text().match(/Next Day/)){daysToShip = 1;}
  			else if ($(this).text().match(/2 Day/)){daysToShip = 2;}
			else if ($(this).text().match(/3 Day/)){daysToShip = 3;}
			else if ($(this).text().match(/4 Day/)){daysToShip = 4;}
			else if ($(this).text().match(/5 Day/)){daysToShip = 5;}
			else if ($(this).text().match(/6 Day/)){daysToShip = 6;}
			else if ($(this).text().match(/7 Day/)){daysToShip = 7;}
			else if ($(this).text().match(/8 Day/)){daysToShip = 8;}
			else if ($(this).text().match(/9 Day/)){daysToShip = 9;}
			else if ($(this).text().match(/10 Day/)){daysToShip = 10;}
		});
		if (daysToShip == 0){
			$('.PriceCalc_dd_SomeOpts').find(':selected').each(function(){			 
				if ($(this).text().match(/1 Day/)){daysToShip = 1;}
				else if ($(this).text().match(/Next Day/)){daysToShip = 1;}
  				else if ($(this).text().match(/2 Day/)){daysToShip = 2;}
				else if ($(this).text().match(/3 Day/)){daysToShip = 3;}
				else if ($(this).text().match(/4 Day/)){daysToShip = 4;}
				else if ($(this).text().match(/5 Day/)){daysToShip = 5;}
				else if ($(this).text().match(/6 Day/)){daysToShip = 6;}
				else if ($(this).text().match(/7 Day/)){daysToShip = 7;}
				else if ($(this).text().match(/8 Day/)){daysToShip = 8;}
				else if ($(this).text().match(/9 Day/)){daysToShip = 9;}
				else if ($(this).text().match(/10 Day/)){daysToShip = 10;}
			});
		}
		$('#estShipHeading').empty();
		if (daysToShip != 0){
			if($('#estShipHeading').length){
				//do nothing
			}else{
				$('#detailPage_shipCalc').prepend('<div id="estShipHeading"></div>');
			}
			var due = new Date();
			
			if (options.debug){$("#calcDebugArea").append('busdays = ' + busdays + "<br/>----<br/>");}
			
			$('#estShipHeading').html(document.createTextNode(options.beforeShipDateText + " " + due.addBusDays(daysToShip + busdays)));
		}
	}

  });// end .each
  
 };//end fn.
 
})(jQuery);